package com.qiqitrue.caipiao.lottery;

import java.util.Date;
import java.util.Set;

import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class GetBaseCQShiCaiList {
	// 随便数集合：可能出现的号码
	static String[] collection = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
	// 随机数个数：模拟的随机数个数
	static int codeCount = 5;
	// 随机数内容
	static Set<String> randomEleSet;
	// 是否每次都随机：中不中都随机
	static boolean nextRandom = false;
	// 成功次数
	static int success = 0;
	// 失败次数
	static int fail = 0;
	// 当前失败次数
	static int thisFail = 0;
	// 最大失败次数
	static int maxFail = 0;
	// 超过失败次数总共有多少次
	static int failCountFlag = 3;
	// 超过失败次数总共有多少次
	static int failCount = 0;
	
	// 模拟金额
	static double myMoney = 10000;
	
	// 每个号多少钱
	static double oneMoney = 0.2;//块
	static double peilv = 9.95;
	static int beishu = 1;
	public static void main(String[] args) {
		/*
		for (int i = 1; i < 10; i++) {
			double mul = NumberUtil.mul(oneMoney, beishu);
			double money = NumberUtil.mul(mul, codeCount);
			myMoney = NumberUtil.sub(myMoney, money);
			System.out.println("第"+i+"次，单注"+oneMoney+"，当前倍数"+beishu+"，总投注"+money+"，剩余余额"+myMoney);
			beishu *= 3;
		}
		*/
		test();
//		getBaseCQShiCaiList();
	}
	
	static void test() {
		Date jdkDate = DateTime.of("2018-01-01", "yyyy-MM-dd").toJdkDate();
		for (int i = 0; i < 10; i++) {
			String date = DateUtil.formatDate(DateUtil.offsetDay(jdkDate, i));
			String file = "D:\\getBaseCQShiCaiList\\" + date + ".txt";
			String pksHistoryListStr = FileUtil.readUtf8String(file);
			execute(pksHistoryListStr);
		}
		double count = NumberUtil.add(success, fail);
		System.out.println("总计：" + count +"\t成功次数=" + success + "\t失败次数=" + fail+"\t最大失败次数="+maxFail+"\t最终余额="+myMoney);
		System.out.println("连续失败"+failCountFlag+"次失败有"+failCount+"次");
	}

	static void execute(String pksHistoryListStr) {
		JSONObject pksHistoryJsonObj = JSONUtil.parseObj(pksHistoryListStr);
		String errorCode = pksHistoryJsonObj.getStr("errorCode");
		if (StrKit.equals("0", errorCode)) {
			JSONArray dataJsonArray = pksHistoryJsonObj.getJSONObject("result").getJSONArray("data");
			int size = dataJsonArray.size() - 1;
			for (int i = size; i >= 0; i--) {
				JSONObject jsonObject = (JSONObject) dataJsonArray.get(i);
				String preDrawIssue = jsonObject.getStr("preDrawIssue");
				String preDrawCode = jsonObject.getStr("preDrawCode");
				String preDrawTime = jsonObject.getStr("preDrawTime");
//				System.out.println("preDrawIssue="+preDrawIssue+"\tpreDrawCode="+preDrawCode+"\t"+preDrawTime);
				String content = StrUtil.split(preDrawCode, ",")[0];
				if(thisFail == 0 || randomEleSet == null || thisFail %3 == 0) {
					
				}
				randomEleSet = RandomUtil.randomEleSet(CollUtil.newHashSet(collection), codeCount);
				
				double mul = NumberUtil.mul(oneMoney, beishu);
				double money = NumberUtil.mul(mul, codeCount);
				if(money > myMoney) {
					System.out.println("资金断裂，退出游戏");
					break;
				}
				myMoney = NumberUtil.sub(myMoney, money);
				System.out.println("第"+preDrawIssue+"期，单注"+oneMoney+"，当前失败次数"+thisFail+"，当前倍数"+beishu+"，总投注"+money+"，内容"+randomEleSet+"，结果["+content+"]，剩余余额"+myMoney);
				/*
				int randomInt = RandomUtil.randomInt(0, 2);
				System.out.println(randomInt);
				// 0表示小
				if(randomInt == 0) {
					randomEleSet = CollUtil.newHashSet(new String[]{ "01", "02", "03", "04", "05"});
				}
				// 1表示大
				else {
					randomEleSet = CollUtil.newHashSet(new String[]{ "06", "07", "08", "09", "10"});
				}
				*/
				
				boolean contains = randomEleSet.contains(content);
				if(contains) {
					success++;
					double add = NumberUtil.mul(mul, peilv);
					myMoney = NumberUtil.add(myMoney, add);
					// 最大失败次数
					if(maxFail < thisFail) {
						maxFail = thisFail;
					}
					if(thisFail > failCountFlag) {
						failCount++;
					}
					// 初始
					thisFail = 0;
					
					beishu = 1;
				}
				else {
					fail++;
					// 当前失败加1
					thisFail ++;
					beishu *= 3;
					/*
					if(thisFail > 2) {
						beishu = 1;
					}
					*/
				}
			}
		}
	}

	/**
	 * 获取历史记录
	 */
	static void getBaseCQShiCaiList() {
		Date jdkDate = DateTime.of("2018-01-01", "yyyy-MM-dd").toJdkDate();
		for (int i = 0; i < 200; i++) {
			String date = DateUtil.formatDate(DateUtil.offsetDay(jdkDate, i));
			String url = "https://api.api68.com/CQShiCai/getBaseCQShiCaiList.do?date=" + date + "&lotCode=10002";
			String pksHistoryListStr = HttpKit.get(url);
			String file = "D:\\getBaseCQShiCaiList\\" + date + ".txt";
			if (StrUtil.isNotBlank(pksHistoryListStr)) {
				if (FileUtil.exist(file)) {
					FileUtil.del(file);
				}
				FileUtil.appendUtf8String(pksHistoryListStr, file);
				System.out.println("写入文件：" + file + "成功!");
			}
		}
	}
}
